package com.feng.leetcode;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author feng
 * @decrition
 * @date 2022/11/1722:56
 */
public class L792 {
    public static int numMatchingSubseq(String s, String[] words) {
        Map<Character, ArrayList<Integer>> map = new HashMap<>();
        for (int i = 0; i < s.length(); i++) {
            ArrayList<Integer> integers = map.get(s.charAt(i));
            if (integers == null) {
                integers = new ArrayList<>();
                integers.add(i);
                map.put(s.charAt(i), integers);
            } else {
                integers.add(i);
            }
        }
        int count = 0;
        List<String> list = new ArrayList<>();
        for (int i = 0; i < words.length; i++) {
            int min = -1;
            boolean flag = true;
            if (list.contains(words[i])) {
                count++;
                continue;
            }
            for (int j = 0; j < words[i].length(); j++) {
                if (map.get(words[i].charAt(j)) == null) {
                    flag = false;
                    break;
                }
                ArrayList<Integer> integers = map.get(words[i].charAt(j));
                boolean temp = true;
                for (Integer integer : integers) {
                    if (integer > min) {
                        min = integer;
                        temp = false;
                        break;
                    }
                }
                if (temp) {
                    flag = false;
                    break;
                }
            }
            if (flag) {
                count++;
                list.add(words[i]);
            }
        }
        return count;
    }

    public static void main(String[] args) {
        numMatchingSubseq("abcde", new String[]{"a","bb","acd","ace"});
    }
}
